package com.springboot.frame.controller;

import com.springboot.frame.config.SolrConfig;
import com.springboot.frame.model.Content;
import org.apache.solr.client.solrj.SolrClient;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList;
import org.apache.solr.common.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * @author liheng
 * @ClassName SearchController
 * @Description
 * @date 2021-03-15 14:20
 */
@Controller
@RequestMapping("/")
public class SearchController {
    @Autowired
    private SolrClient solrClient;

    @RequestMapping("search")
    public String search(@RequestParam("text") String text, HttpServletRequest request) throws IOException, SolrServerException {
        SolrQuery solrQuery = new SolrQuery();
        if (StringUtils.isEmpty(text)) {
            solrQuery.setQuery("*:*");
        } else {
            solrQuery.setQuery("descs:" + text);
        }
        //设置查询的条数
        solrQuery.setRows(50);
        //设置查询的开始
        solrQuery.setStart(0);
        //设置高亮
        solrQuery.setHighlight(true);
        //设置高亮的字段
        solrQuery.addHighlightField("descs");
        //设置高亮的样式
        solrQuery.setHighlightSimplePre("<font color='red'>");
        solrQuery.setHighlightSimplePost("</font>");

        QueryResponse queryResponse = solrClient.query(solrQuery);
        SolrDocumentList results = queryResponse.getResults();
        // 下面方法不可用
        // List<Content> list = queryResponse.getBeans(Content.class);
        Map<String, Map<String, List<String>>> highlight = queryResponse.getHighlighting();

        for (SolrDocument result : results) {
            result.setField("descs", highlight.get(result.get("id")).get("descs").get(0));
        }
        request.setAttribute("list", results);
        return "search";
    }

    @ResponseBody
    @RequestMapping("/create")
    public String createIndex() throws IOException, SolrServerException {
        List<Content> list1 = SolrConfig.INDEX_DATA;
        solrClient.addBeans(list1);
        solrClient.commit();
        return "success";
    }

    /**
     * 删除所有的索引
     *
     * @return
     */
    @ResponseBody
    @RequestMapping("deleteAll")
    public String deleteAll() {
        try {
            solrClient.deleteByQuery("*:*");
            solrClient.commit();
            return "success";
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "error";
    }


    /**
     * 综合查询: 在综合查询中, 有按条件查询, 条件过滤, 排序, 分页, 高亮显示, 获取部分域信息
     *
     * @return
     */
    /*@RequestMapping("search")
    public Map<String, Map<String, List<String>>> search() {

        try {
            SolrQuery params = new SolrQuery();

            //查询条件, 这里的 q 对应 下面图片标红的地方
            params.set("q", "手机");

            //过滤条件
            params.set("fq", "product_price:[100 TO 100000]");

            //排序
            params.addSort("product_price", SolrQuery.ORDER.asc);

            //分页
            params.setStart(0);
            params.setRows(20);

            //默认域
            params.set("df", "product_title");

            //只查询指定域
            params.set("fl", "id,product_title,product_price");

            //高亮
            //打开开关
            params.setHighlight(true);
            //指定高亮域
            params.addHighlightField("product_title");
            //设置前缀
            params.setHighlightSimplePre("<span style='color:red'>");
            //设置后缀
            params.setHighlightSimplePost("</span>");

            QueryResponse queryResponse = client.query(params);

            SolrDocumentList results = queryResponse.getResults();

            long numFound = results.getNumFound();

            System.out.println(numFound);

　　　　　　　//获取高亮显示的结果, 高亮显示的结果和查询结果是分开放的
            Map<String, Map<String, List<String>>> highlight = queryResponse.getHighlighting();

            for (SolrDocument result : results) {
                System.out.println(result.get("id"));
                System.out.println(result.get("product_title"));
                //System.out.println(result.get("product_num"));
                System.out.println(result.get("product_price"));
                //System.out.println(result.get("product_image"));

                Map<String, List<String>> map = highlight.get(result.get("id"));
                List<String> list = map.get("product_title");
                System.out.println(list.get(0));

                System.out.println("------------------");
                System.out.println();
            }
            return highlight;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }*/

}
